﻿package {
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	import Box2D.Dynamics.Joints.*;
	import flash.ui.GameInput;

	public class gamestate{
		
		var gameCircle:b2Body;
		var edges:Array;
		var currentEdge:edge;
		var stepsSinceStart:int;
		var circleStart:b2Vec2;
		
		public function gamestate(currentCircle:b2Body, currentEdgeList:Array, stepsSinceStart:int, circleStart:b2Vec2):void {
			gameCircle = currentCircle;
			edges = new Array();
			edges = edges.concat(currentEdgeList);
			this.stepsSinceStart = stepsSinceStart;
			this.circleStart = circleStart;
		}
		
		public function getAngularVelocity():Number {
			return gameCircle.GetAngularVelocity();
		}
		
		public function getLinearVelocity():b2Vec2 {
			return gameCircle.GetLinearVelocity();
		}
		
		public function getPositionX():Number {
			return gameCircle.GetPosition().x;
		}
		
		public function getPositionY():Number {
			return gameCircle.GetPosition().y;
		}
		
		public function getUntouchedEdges():Array {
			var untouchedEdges:Array = new Array();
			for (var i:Number = 0; i < edges.length; i++) {
				if ((edges[i] as edge).getTimesTouched() == 0) {
					untouchedEdges.push(edges[i]);
				}
			}
			//trace("Untouched: " + untouchedEdges.length);
			var returnArray = new Array();
			returnArray = returnArray.concat(untouchedEdges);
			return returnArray;
		}
		
		public function getCurrentEdge():edge {
			return currentEdge;
		}
		
		public function setCurrentEdge(edgeToSet:edge):void {
			currentEdge = edgeToSet;
		}
		public function getStepsSinceStart():int {
			return this.stepsSinceStart;
		}
		public function getManhattanDistanceFromStart():Number {
			return Math.abs(gameCircle.GetPosition().x - circleStart.x) + Math.abs(gameCircle.GetPosition().y - circleStart.y);
		}
		public function getEdgeList():Array {
			return edges;
		}
		public function setEdgeList(edges:Array):void {
			this.edges = new Array();
			this.edges = this.edges.concat(edges);
		}
		
		public function equals(testGameState:gamestate):Boolean {
			//trace("begin");
			for (var i = 0; i < this.edges.length; i++) {
				var thisEdgeTest:edge = this.edges[i] as edge;
				var foundMatch:Boolean = false;
				for (var j = 0; j < testGameState.edges.length; j++) {
					var testEdgeTest:edge = testGameState.edges[j] as edge;
					if (thisEdgeTest.equals(testEdgeTest)) {
						foundMatch = true;
					}
				}
				if (!foundMatch) {
					//trace("returning");
					return false;
				}
			}
			if (Math.floor(gameCircle.GetPosition().x) == Math.floor(testGameState.gameCircle.GetPosition().x) &&
				Math.floor(gameCircle.GetPosition().y) == Math.floor(testGameState.gameCircle.GetPosition().y) &&
				Math.floor(gameCircle.GetAngularVelocity()) == Math.floor(testGameState.gameCircle.GetAngularVelocity()) &&
				Math.floor(gameCircle.GetLinearVelocity().Normalize()) == Math.floor(testGameState.gameCircle.GetLinearVelocity().Normalize())
				) {
					//trace("returning");
					return true;
			} else {
				//trace("returning");
				return false;
			}
		}
	}	
}